package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.DiagnosisListService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.masterData.DiagnosisListDelDto;
import com.ccys.db.dto.masterData.DiagnosisListPageQueryDto;
import com.ccys.db.dto.masterData.DiagnosisListSaveDto;
import com.ccys.db.entity.TblDiagnosisList;
import com.ccys.db.entity.TblDiagnosisListDetail;
import com.ccys.db.mapper.TblDiagnosisListDetailMapper;
import com.ccys.db.mapper.TblDiagnosisListMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.DiagnosisListPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 诊断集合实现类
 * @author: ZhangJingCan
 * @create: 2023-12-12 17:30
 **/

@Service
public class DiagnosisListServiceImpl extends BaseServiceImpl<TblDiagnosisList> implements DiagnosisListService {

    @Resource
    private TblDiagnosisListMapper tblDiagnosisListMapper;

    @Resource
    private TblDiagnosisListDetailMapper tblDiagnosisListDetailMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveDiagnosisList(DiagnosisListSaveDto dto) {
        //查询是否存在同名并且未删除的数据
        if (ObjectUtil.isNotEmpty(dto.getName())) {
            //准备条件
            Example example = new Example(TblDiagnosisList.class);
            example.createCriteria()
                    .andEqualTo("name", dto.getName())
                    .andEqualTo("delFlag", 0);
            //校验ID如果存在则是修改 - 修改需要排除自身
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                example.createCriteria().andNotEqualTo("id", dto.getId());
            }
            //查询并判断
            if (ObjectUtil.isNotEmpty(tblDiagnosisListMapper.selectOneByExample(example))) {
                return ResponseResult.error("DiagnostisList name with the same name already exists!");
            }
        }
        if (ObjectUtil.isNotEmpty(dto.getId()) && ObjectUtil.isNotEmpty(dto.getStatus())) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
        }
        //新增/修改并返回状态
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteDiagnosisList(DiagnosisListDelDto dto) {
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //删除集合需要查询是否存在当前集合的详情数据 - 查询详情数据
        Example example = new Example(TblDiagnosisListDetail.class);
        example.createCriteria()
                .andEqualTo("listId", dto.getId())
                .andEqualTo("delFlag", 0);
        int count = tblDiagnosisListDetailMapper.selectCountByExample(example);
        if (count > 0) {
            return ResponseResult.error("There are still " + count + " pieces of data in the current set that cannot be deleted. Please delete the data in the set before attempting to delete the set");
        }
        //准备删除集合
        TblDiagnosisList list = new TblDiagnosisList();
        list.setId(dto.getId());
        list.setDelFlag(1);
        //操作数据库并返回
        if (tblDiagnosisListMapper.updateByPrimaryKeySelective(list) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult findPageDiagnosisList(DiagnosisListPageQueryDto dto) {
        //检查字段值不在范围则赋值null
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        List<DiagnosisListPageQueryVo> list = tblDiagnosisListMapper.findPage(dto);
        PageInfo<DiagnosisListPageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }
}
